package com.nd.Utils;

import com.nd.dao.CouponMapper;
import com.nd.dao.CouponUserMapper;
import com.nd.pojo.bean.Coupon;
import com.nd.pojo.bean.CouponExample;
import com.nd.pojo.bean.CouponUser;
import com.nd.pojo.bean.CouponUserExample;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class CouponUtils {

    // couponStatus
    private static Short couponStatus0 = 0; //可正常使用
    private static Short couponStatus1 = 1; //已过期
    private static Short couponStatus2 = 2; //已下架

    // CouponUserStatus
    private static Short couponUserStatus0 = 0; //未使用
    private static Short couponUserStatus1 = 1; //已使用
    private static Short couponUserStatus2 = 2; //已过期
    private static Short couponUserStatus3 = 3; //已下架

    public static void couponRefresh(CouponMapper couponMapper, CouponUserMapper couponUserMapper, Integer userId){
        // 先检查coupon和couponUser两个表的优惠券是否有过期的
        CouponExample couponExample10 = new CouponExample();
        CouponExample.Criteria criteria10 = couponExample10.createCriteria();

        List<Coupon> coupons10 = couponMapper.selectByExample(couponExample10);

        for (Coupon coupon : coupons10) {
            Date date10 = new Date();
            if (coupon.getTimeType() == 1){
                if (date10.after(coupon.getEndTime())){
                    coupon.setStatus(couponStatus1);
                    //coupon.setDeleted(true);
                    CouponExample couponExample = new CouponExample();
                    CouponExample.Criteria criteria = couponExample.createCriteria();
                    criteria.andIdEqualTo(coupon.getId());
                    couponMapper.updateByExampleSelective(coupon, couponExample);
                }
            }

        }
        // 当前用户 过期的购物券刷新
        CouponUserExample couponUserExample11 = new CouponUserExample();
        CouponExample.Criteria criteria11= couponExample10.createCriteria();

        criteria11.andIdEqualTo(userId);

        List<CouponUser> couponUsers11 = couponUserMapper.selectByExample(couponUserExample11);

        for (CouponUser couponUser : couponUsers11) {
            Date date = new Date();
            if(date.after(couponUser.getEndTime())){
                couponUser.setStatus(couponUserStatus2);
                CouponUserExample couponUserExample = new CouponUserExample();
                CouponUserExample.Criteria criteria1 = couponUserExample.createCriteria();
                criteria1.andIdEqualTo(couponUser.getId());
                couponUserMapper.updateByExampleSelective(couponUser, couponUserExample);

                //couponUser.setDeleted(true);
            }
        }

        // -----刷新结束
    }

    public static void conponOnlyRefresh (CouponMapper couponMapper){

        CouponExample couponExample10 = new CouponExample();
        CouponExample.Criteria criteria10 = couponExample10.createCriteria();

        List<Coupon> coupons10 = couponMapper.selectByExample(couponExample10);

        for (Coupon coupon : coupons10) {
            Date date10 = new Date();
            if (coupon.getTimeType() == 1){
                if (date10.after(coupon.getEndTime())){
                    coupon.setStatus(couponStatus1);
                    //coupon.setDeleted(true);
                    CouponExample couponExample = new CouponExample();
                    CouponExample.Criteria criteria = couponExample.createCriteria();
                    criteria.andIdEqualTo(coupon.getId());
                    couponMapper.updateByExampleSelective(coupon, couponExample);
                }
            }

        }



    }


}
